﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PageNumAction = System.Action<long>;

namespace Nana.View.Utils
{
  public static class PageNumExt
  {
    /// <summary>
    /// 使用传入的委托计算并产生友好的页码。
    /// </summary>
    /// <param name="o"></param>
    /// <param name="currentPage">当前页码</param>
    /// <param name="actionPageNum">处理页码的方式</param>
    /// <param name="actionFolding">处理被折叠页码的方式</param>
    /// <param name="maxSiblings">临近页码的数量</param>
    /// <param name="preventFolding">阻止折叠页码的临界值，超过这个数目的页码将会被折叠</param>
    public static void PageNumView(this object o,
      long currentPage, PageNumAction actionPageNum, Action actionFolding,
      long maxSiblings = 2, long preventFolding = 1)
    {
      o.PageNumView(currentPage, actionPageNum, actionPageNum, actionFolding, maxSiblings, preventFolding);
    }

    /// <summary>
    /// 使用传入的委托计算并产生友好的页码。当前页码将会被特别对待。
    /// </summary>
    /// <param name="o"></param>
    /// <param name="currentPage">当前页码</param>
    /// <param name="actionCurrent">处理当前页码的方式</param>
    /// <param name="actionPageNum">处理页码的方式</param>
    /// <param name="actionFolding">处理被折叠页码的方式</param>
    /// <param name="maxSiblings">临近页码的数量</param>
    /// <param name="preventFolding">阻止折叠页码的临界值，超过这个数目的页码将会被折叠</param>
    public static void PageNumView(this object o,
      long currentPage, PageNumAction actionCurrent,
      PageNumAction actionPageNum, Action actionFolding,
      long maxSiblings = 2, long preventFolding = 1)
    {
      o.PageNumView(
        currentPage, actionCurrent,
        1, long.MaxValue,
        actionPageNum, i => { },
        maxSiblings, actionPageNum,
        preventFolding, actionFolding,
        maxSiblings, 0,
        actionPageNum, i => { }
        );
    }

    /// <summary>
    /// 根据传入的参数和委托来计算并产生友好的页码。当前页码将会被特别对待。
    /// </summary>
    /// <param name="o"></param>
    /// <param name="currentPage">当前页码</param>
    /// <param name="actionCurrent">处理当前页码的方式</param>
    /// <param name="beginPage">预设的起始页码</param>
    /// <param name="endPage">预设的结束页码</param>
    /// <param name="actionPageNum">处理页码的方式</param>
    /// <param name="actionFolding">处理被折叠页码的方式</param>
    /// <param name="maxSiblings">临近页码的数量</param>
    /// <param name="preventFolding">阻止折叠页码的临界值，超过这个数目的页码将会被折叠</param>
    public static void PageNumView(this object o,
      long currentPage, PageNumAction actionCurrent,
      long beginPage, long endPage,
      PageNumAction actionPageNum, Action actionFolding,
      long maxSiblings = 2, long preventFolding = 1)
    {
      o.PageNumView(currentPage, actionCurrent, beginPage, endPage, actionPageNum, actionPageNum, actionPageNum, actionFolding, maxSiblings, preventFolding);
    }

    /// <summary>
    /// 根据传入的参数和委托来计算并产生友好的页码。当前页码、起始页码和终止页码都指定了专用的委托来处理。
    /// </summary>
    /// <param name="o"></param>
    /// <param name="currentPage">当前页码</param>
    /// <param name="actionCurrent">处理当前页码的方式</param>
    /// <param name="beginPage">预设的起始页码</param>
    /// <param name="endPage">预设的结束页码</param>
    /// <param name="actionBegin">处理起始页码的方式</param>
    /// <param name="actionEnd">处理结束页码的方式</param>
    /// <param name="actionSebling">处理临近页码的方式</param>
    /// <param name="actionFolding">处理被折叠页码的方式</param>
    /// <param name="maxSiblings">临近页码的数量</param>
    /// <param name="preventFolding">阻止折叠页码的临界值，超过这个数目的页码将会被折叠</param>
    public static void PageNumView(this object o,
      long currentPage, PageNumAction actionCurrent,
      long beginPage, long endPage,
      PageNumAction actionBegin, PageNumAction actionEnd,
      PageNumAction actionSebling, Action actionFolding,
      long maxSiblings = 2, long preventFolding = 1)
    {
      o.PageNumView(
        currentPage, actionCurrent,
        beginPage, endPage,
        actionBegin, actionEnd,
        maxSiblings, actionSebling,
        preventFolding, actionFolding,
        maxSiblings, maxSiblings,
        actionSebling, actionSebling
        );
    }

    /// <summary>
    /// 根据传入的参数和委托来计算并产生友好的页码。每一种页码的具体处理方式都需要由调用方指派委托。
    /// </summary>
    /// <param name="o"></param>
    /// <param name="currentPage">当前页码</param>
    /// <param name="actionCurrent">处理当前页码的方式</param>
    /// <param name="beginPage">预设的起始页码</param>
    /// <param name="endPage">预设的结束页码</param>
    /// <param name="actionBegin">处理起始页码的方式</param>
    /// <param name="actionEnd">处理结束页码的方式</param>
    /// <param name="currentSiblings">当前页码的临近页码数量</param>
    /// <param name="actionCurrentSibling">处理临近页码的方式</param>
    /// <param name="preventFolding">阻止折叠页码的临界值，超过这个数目的页码将会被折叠</param>
    /// <param name="actionFolding">处理被折叠页码的方式</param>
    /// <param name="endOfBegin">起始页临近页码的数量</param>
    /// <param name="beginOfEnd">结束页临近页码的数量</param>
    /// <param name="actionBeginSibling">处理起始页临近页码的方式</param>
    /// <param name="actionEndSibling">处理结束页临近页码的方式</param>
    public static void PageNumView(this object o,
      long currentPage, PageNumAction actionCurrent,
      long beginPage, long endPage,
      PageNumAction actionBegin, PageNumAction actionEnd,
      long currentSiblings, PageNumAction actionCurrentSibling,
      long preventFolding, Action actionFolding,
      long endOfBegin, long beginOfEnd,
      PageNumAction actionBeginSibling, PageNumAction actionEndSibling
      )
    {
      long i = beginPage;

      // 如果起始页小于当前页，就开始处理起始页
      if (beginPage < currentPage)
      #region
      {
        actionBegin(beginPage);
        i++;

        endOfBegin += i; // 解决靠近起始页的页码之后
        var siblingBegin = currentPage - currentSiblings; // 当前页的临近页从何处开始
        var foldingStart = siblingBegin - preventFolding; // 页码折叠的底线

        if (endOfBegin > siblingBegin)
          endOfBegin = siblingBegin; // 保证当前页的临近页，牺牲起始页的临近页

        for (; i < endOfBegin; i++)
          actionBeginSibling(i);

        if (i < foldingStart) // 如果还没到达折叠底线，就马上开始折叠
        {
          actionFolding();
          i = foldingStart + 1; // 跳到折叠之后的页码
        }
        //else // 不折叠，当作临近页码处理
        //  for (; i < siblingBegin; i++) // 到当前页的临近页码之前
        //    actionCurrentSibling(i);
      }
      #endregion

      // 处理当前页之前的临近页
      for (; i < currentPage; i++)
        actionCurrentSibling(i);

      // 处理当前页
      actionCurrent(currentPage);
      i = currentPage + 1; // 表示当前页已经处理完成。
      // 由于不能保证当前页和期望的起始页的关系，所以在此从当前页开始计算。

      var goal = i + currentSiblings; // 设定一个目标
      if (goal > endPage) // 目标不要超过结束页
        goal = endPage;

      // 处理当前页之后的临近页，直到到达目标页为止
      for (; i < goal; i++)
        actionCurrentSibling(i);

      // 如果结束页大于当前页，就开始处理结束页
      if (endPage > currentPage)
      #region
      {
        beginOfEnd = endPage - beginOfEnd; // 计算出结束页的临近页从何处开始
        var foldingStart = beginOfEnd - preventFolding;

        //if (goal < foldingStart)
        if (i < foldingStart) // 没有触及折叠底线，马上开始折叠
        {
          actionFolding();

          //if (i < beginOfEnd) // 如果小于
          i = beginOfEnd;
        }
        else // 全部当作当前页的临近页来处理，但保留结束页的临近页
          for (; i < beginOfEnd; i++)
            actionCurrentSibling(i);


        for (; i < endPage; i++)
          actionEndSibling(i);
        actionEnd(endPage);
      }
      #endregion

    }
  }
}